package com.bytedance.article.common.launchcrash;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class a implements d, Thread.UncaughtExceptionHandler {
    public static final class a$a {
        public a$a() {

        }
    }

    final static class b implements Runnable {
        private final a c;
        private CountDownLatch a;
        private JSONObject b;

        public b(a arg2, CountDownLatch arg3, JSONObject arg4) {

            this.c = arg2;
            this.a = null;
            this.b = null;
            this.a = arg3;
            this.b = arg4;
        }

        public final void run() {
            try {
                JSONObject v0_2 = this.b;
                if (com.bytedance.article.common.launchcrash.a.b(this.c) != null) {
                    com.bytedance.article.common.launchcrash.a.b(this.c).a(v0_2, com.bytedance.article.common.launchcrash.a.c(this.c));
                }

                if (com.bytedance.article.common.launchcrash.a.a(this.c) != null && this.b != null) {
                    com.bytedance.article.common.launchcrash.b v0_3 = com.bytedance.article.common.launchcrash.a.a(this.c);
                    this.b.optString("stack");
                    v0_3.a();
                }
            } catch (Exception v0_1) {
                return;
            } finally {
                if (this.a != null) {
                    this.a.countDown();
                }
            }
        }
    }

    private Context a;
    private SharedPreferences b;
    private Thread.UncaughtExceptionHandler c;
    private boolean d;
    private volatile long e;
    private static volatile a f;
    private volatile a$a g;
    private volatile boolean h;
    private volatile e i;
    private com.bytedance.article.common.launchcrash.b j;

    private a(Context arg5, a$a arg6) {

        this.a = null;
        this.b = null;
        this.c = null;
        this.d = false;
        this.e = 0;
        this.a = arg5;
        String v1 = com.bytedance.article.common.launchcrash.g.b(arg5);
        String v0 = "sp_crash_catch_recognizer";
        if (!TextUtils.isEmpty(((CharSequence) v1))) {
            v0 = "sp_crash_catch_recognizer" + v1;
        }

        this.b = arg5.getSharedPreferences(v0, 0);
        this.d = this.b.getBoolean("launch_crash", false);
        this.b.getInt("launch_crash_times", 0);
        this.g = arg6;
        this.i = new e(arg5);
    }

    static a a(Context arg2, a$a arg3) {
        if (com.bytedance.article.common.launchcrash.a.f == null) {
            Class v1 = a.class;
            synchronized (v1) {
                if (com.bytedance.article.common.launchcrash.a.f == null) {
                    com.bytedance.article.common.launchcrash.a.f = new a(arg2, arg3);
                }
            }
        }

        return com.bytedance.article.common.launchcrash.a.f;
    }

    static com.bytedance.article.common.launchcrash.b a(a arg1) {
        return arg1.j;
    }

    private synchronized void a(Throwable arg5) {
        System.currentTimeMillis();
        JSONObject v0_1 = this.b(arg5);
        if (Looper.getMainLooper() != Looper.myLooper()) {
            if (this.i != null) {
                this.i.a(v0_1, this.g);
            }

            if (this.j == null) {
                return;
            }

            com.bytedance.article.common.launchcrash.b v1_1 = this.j;
            v0_1.optString("stack");
            v1_1.a();
            return;
        }

        CountDownLatch v1 = new CountDownLatch(1);
        new Thread(new b(this, v1, v0_1)).start();
        long v2 = 4000;
        try {
            v1.await(v2, TimeUnit.MILLISECONDS);
        } catch (InterruptedException v0_2) {
            v0_2.printStackTrace();
        }
    }

    public final void a() {
        if (!this.h) {
            this.e = System.currentTimeMillis();
            this.c = Thread.getDefaultUncaughtExceptionHandler();
            if (this == this.c) {
                this.c = null;
            } else {
                Thread.setDefaultUncaughtExceptionHandler(((Thread.UncaughtExceptionHandler) this));
            }

            this.h = true;
        }
    }

    public final void a(com.bytedance.article.common.launchcrash.b arg1) {
        this.j = arg1;
    }

    private JSONObject b(Throwable arg6) {
        JSONObject v1 = new JSONObject();
        try {
            v1.put("launch_did", com.bytedance.article.common.launchcrash.c.a(this.a));
            String v0_1 = "stack";
            StringWriter v2 = new StringWriter();
            PrintWriter v3 = new PrintWriter(((Writer) v2));
            arg6.printStackTrace(v3);
            Throwable v4 = arg6.getCause();
            if (v4 != null) {
                v4.printStackTrace(v3);
                v4 = v4.getCause();
                if (v4 != null) {
                    v4.printStackTrace(v3);
                }
            }

            String v2_1 = v2.toString();
            v3.close();
            v1.put(v0_1, v2_1);
            v1.put("timestamp", System.currentTimeMillis());
            v1.put("event_type", "start_crash");
            v1.put("main_process", com.bytedance.article.common.launchcrash.g.a(this.a));
        } catch (JSONException v0) {
            v0.printStackTrace();
        }

        return v1;
    }

    static e b(a arg1) {
        return arg1.i;
    }

    private synchronized void b() {
        this.d = true;
        SharedPreferences.Editor v0_1 = this.b.edit();
        v0_1.putBoolean("launch_crash", this.d);
        v0_1.putInt("launch_crash_times", 1);
        v0_1.commit();
    }

    static a$a c(a arg1) {
        return arg1.g;
    }

    public void uncaughtException(Thread arg5, Throwable arg6) {
        if (System.currentTimeMillis() - this.e <= 8000) {
            this.b();
            this.a(arg6);
        }

        if (this.c != null) {
            this.c.uncaughtException(arg5, arg6);
        }
    }
}

